//
//  StatusPictureView.swift
//  SinaWeibo
//
//  Created by yang on 15/11/14.
//  Copyright © 2015年 yang. All rights reserved.
//

import UIKit
import SDWebImage
private let PictureCellId = "PictureCellId"
private let PictureCellMargin: CGFloat = 5
class StatusPictureView: UICollectionView {
    var imageURLs: [NSURL]?{
        didSet{
            reloadData()
            let viewSize = self.calceViewSize()
            self.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(viewSize.width)
                make.height.equalTo(viewSize.height)
            }
        }
    }
    private func calceViewSize() -> CGSize{
        let colum:CGFloat = 3.000000001
        let maxWidth = screenW - 2 * StatusCellMargin
        let itemWidth = (maxWidth - 2 * PictureCellMargin) / colum
        let layout = self.collectionViewLayout as!UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        let imageCount = imageURLs?.count ?? 0
        if imageCount == 0 {
        return CGSizeZero
        }
        if imageCount == 1 {
            let key = imageURLs?.first?.absoluteString
            let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(key!)
            let imageSize = image.size
            //let imageSize = CGSize(width: 120, height: 180)
            layout.itemSize = imageSize
            return imageSize
        }
        if imageCount == 4 {
        let w = itemWidth * 2 + PictureCellMargin
            return CGSize(width: w, height: w)
        }
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let w = maxWidth
        let h = row * itemWidth + (row - 1) * PictureCellMargin
        return CGSize(width: w, height: h)
    }
    init(){
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = PictureCellMargin
        layout.minimumInteritemSpacing = PictureCellMargin
    super.init(frame: CGRectZero, collectionViewLayout: layout)
        //注册 cell
        registerClass(PictureViewCell.self, forCellWithReuseIdentifier: PictureCellId)
        self.backgroundColor = UIColor.whiteColor()
        dataSource = self
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
//数据源方法
extension StatusPictureView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureCellId, forIndexPath: indexPath) as! PictureViewCell
        cell.backgroundColor = UIColor.randomColor()
        cell.url = imageURLs![indexPath.item]
        return cell
    }
}
//自定义collectionViewCell
class PictureViewCell:UICollectionViewCell{
    var url:NSURL?{
        didSet{
        imageView.sd_setImageWithURL(url, placeholderImage: nil)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
       contentView.addSubview(imageView)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
    }
    private lazy var imageView :UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
        }()
}